'use client'; import { useEffect, useMemo, useState } from 'react'; import Image from 'next/image'; import { ChevronLeft, ChevronRight, Play } from 'lucide-react'; import { youTubeId } from '@/lib/utils/youtube'; type MediaItem = | { type: 'video'; embed: string; thumb: string } | { type: 'image'; src: string }; type Props = { trailerUrl: string|null; images: string[]; }; export default function GameMediaGallery({ trailerUrl, images }: Props) { const items = useMemo(() => { const list: MediaItem[] = []; const id = youTubeId(trailerUrl); if (id) { list.push({ type: 'video', embed: `https://www.youtube.com/embed/${id}`, thumb: `https://img.youtube.com/vi/${id}/hqdefault.jpg` }); } for (const src of images) { list.push({ type: 'image', src }); } return list; }, [trailerUrl, images]); const [active, setActive] = useState(0); useEffect(() => { setActive(0); }, [items.length]); if (items.length === 0) { return null; } const index = Math.min(active, items.length - 1); const current = items[index]; const hasVideo = items[0]?.type === 'video'; const move = (delta: number) => { setActive(prev => (prev + delta + items.length) % items.length); }; return (
{current.type === 'video' ? (